//
//  UserSettings.swift
//  GitTFGUI
//
//  Created by Samuel Richard Magny on 2015-08-29.
//  Copyright (c) 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa

public class UserSettings: NSUserDefaults {

    private let usernameKey = "username"
    private let passwordKey = "password"
    private let emailKey = "email"
    
    private let workingDirectoryKey = "workingDir"
    private let serverUrlKey = "serverUrl"
    private let projectPathKey = "projPath"
    
    private let preferedGitClientKey:String = "preferedGitClient"
    
    
    private var userDefault: NSUserDefaults! = NSUserDefaults.standardUserDefaults()
    
    public static let defaultUserSettings = UserSettings()
    
    public func resetSettings() {
        userDefault.removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)
    }
    
    public var username:String {
        get{
            let username = userDefault.stringForKey(usernameKey)
            if(username == nil) {
                return ""
            }
            else {
                return username!
            }
        }
        set (username){
            userDefault.setObject(username, forKey: usernameKey)
        }
    }
    
    public var password:String {
        get{
            let password = userDefault.stringForKey(passwordKey)
            if(password == nil) {
                return ""
            }
            else {
                return password!
            }
        }
        set (password){
            userDefault.setObject(password, forKey: passwordKey)
        }
    }
    public var email:String {
        get{
            let email = userDefault.stringForKey(emailKey)
            if(email == nil) {
                return ""
            }
            else {
                return email!
            }
        }
        set (email){
            userDefault.setObject(email, forKey: emailKey)
        }
    }
    
    public var workingDirectory:String {
        get{
            let workDir = userDefault.stringForKey(workingDirectoryKey)
            if(workDir == nil) {
                return ""
            }
            else {
                return workDir!
            }
        }
        set (workDir){
            userDefault.setObject(workDir, forKey: workingDirectoryKey)
        }
    }
    
    public var serverUrl:String {
        get{
            let serverUrlStr = userDefault.stringForKey(serverUrlKey)
            if(serverUrlStr == nil) {
                return ""
            }
            else {
                return serverUrlStr!
            }
        }
        set (serverUrlStr){
            userDefault.setObject(serverUrlStr, forKey: serverUrlKey)
        }
    }
    
    public var projectPath:String {
        get{
            let projPath = userDefault.stringForKey(projectPathKey)
            if(projPath == nil) {
                return ""
            }
            else {
                return projPath!
            }
        }
        set (projPath){
            userDefault.setObject(projPath, forKey: projectPathKey)
        }
    }
    
    public var preferedGitClient:Int {
        get{
            let client = userDefault.integerForKey(preferedGitClientKey)

            return client
        }
        set (client){
            userDefault.setObject(client, forKey: preferedGitClientKey)
        }
    }
    
}
